import java.io.*;
import java.net.*;
import java.util.*;
import java.security.Security;
import java.security.Provider;
import javax.net.ssl.*;
import local.util.MyPrint;
import java.text.DecimalFormat;
import java.lang.Long;

import local.util.MyStringFormat;
import local.util.MyPrint;

public class NasdaqReport {    

    private Vector nazRecordVector = new Vector ();

    DecimalFormat td = new DecimalFormat("0.00");
    DecimalFormat td1 = new DecimalFormat("0");
    MyStringFormat mf = new MyStringFormat();

    public static void main (String args[]) {

	if (args.length<1) {
	    MyPrint.printLine("=",30);
	    System.out.println("2002-09-01");
	    MyPrint.printLine("=",30);
	    System.exit(0);
	}

	NasdaqReport nazreport = new NasdaqReport ();
	nazreport.setNazRecordVector(args[0]);
	nazreport.printNazReport (nazreport.getNazRecord());
    }

    public void setNazRecordVector (String start_date) {
	Vector t_head_v   = new Vector();
	Vector t_record_v = new Vector();
	Vector record_v   = new Vector ();
	Hashtable t_head_h = new Hashtable ();
	/*
	DownloadYahooData dy = new DownloadYahooData ();
	dy.downloadData ();
	*/
	String my_query_string ="SELECT * FROM Nasdaq WHERE DATE >= #"+start_date+"# ORDER BY DATE";

	DataBaseQuery my_query = new DataBaseQuery ();

	my_query.setQueryString (my_query_string);
	my_query.lookup (my_query_string);

	t_head_v    = my_query.getTableHeadVector();
	t_record_v  = my_query.getTableRecordVector();

	setNazRecord(t_record_v);
    }

    public void setNazRecord (Vector d_v_v) {

	Vector temp_v = new Vector();

	Enumeration enum = d_v_v.elements();
	while ( enum.hasMoreElements()) {
	    temp_v =(Vector)enum.nextElement();
	    
	    NasdaqRecord n_r = new NasdaqRecord ();
	    n_r.setTick("Nasdaq");
	    Enumeration enum1 = temp_v.elements();
	    while ( enum1.hasMoreElements()) {
		n_r.setDate  ( (String)enum1.nextElement() );
		n_r.setOpen  (((Long)enum1.nextElement()).longValue());
		n_r.setHigh  (((Long)enum1.nextElement()).longValue());
		n_r.setLow   (((Long)enum1.nextElement()).longValue());
		n_r.setClose (((Long)enum1.nextElement()).longValue());
		n_r.setVolume     (Integer.parseInt(enum1.nextElement().toString()));
	    }
	    nazRecordVector.add(n_r);
	}
    }      

    public Vector getNazRecordVector () {
	return nazRecordVector;
    }
 
    public Vector getNazRecord ( ) {

	return nazRecordVector;
    }    

    public void printNazReport( Vector record_v) {

	long last_close = 0;
	long vxc        = 0;
	int  nc         = 0;
	int change      = 0;

	Enumeration enum = record_v.elements();
	NasdaqRecord nr = new NasdaqRecord ();

	System.out.println("Tick  "+"     Date  "+"    OPEN "+"      HIGH "+"   LOW "+"    CLOSE "+"     MAV "+"   VOLUME   "+"        MONEY  "+"         Total"+ "      Change");

	while ( enum.hasMoreElements()) { 
	    nr = (NasdaqRecord)enum.nextElement();

	    System.out.print(nr.getTick()+"  ");
	    System.out.print((nr.getDate()).replaceAll("00:00:00","")+"  ");
	    System.out.print(nr.getOpen()+"\t");
	    System.out.print(nr.getHigh()+"\t");
	    System.out.print(nr.getLow()+"\t");
	    System.out.print(nr.getClose()+"\t");
	    System.out.print(mf.rightIndent(getMoveAveg(nr.getDate(), 3, getNazRecord()),6, ' '));
	    System.out.print(mf.rightIndent(nr.getVolume(),12,' ')+"      ");
	    change = (int)(nr.getClose() - last_close);

	    if (last_close == 0) {
		change = 0;
	    }

	    vxc += (nr.getVolume()/1000000)*change;
	    nc  += change;

	    System.out.print("$"+mf.rightIndent(vxc,8,' ')+"      ");
	    System.out.print(mf.rightIndent(nc,8,' ')+"      ");

	    if (last_close == 0) {
		last_close = nr.getClose();
		System.out.println("\t");
	    }
	    else {
		if (change >= 0) {
		    System.out.println("+"+ change +"\t");
		}
		else {
		    System.out.println(change +"\t");
		}
		last_close = nr.getClose();
	    }

	}
    }
    
    public int getMoveAveg(String current_date, int mv_date, Vector naz_record_v) {

	int mt     = 0;
	int ma     = 0;
	int j      = 0;

	Enumeration enum = naz_record_v.elements();
	NasdaqRecord nr0 = new NasdaqRecord ();
	
	while ( enum.hasMoreElements()) { 
	    nr0 = (NasdaqRecord)enum.nextElement();

	    if ((nr0.getDate()).startsWith(current_date)) {
		for (j=0; j<mv_date; j++) {
		    if (naz_record_v.indexOf((Object)nr0)>j) {
			mt += (int)((NasdaqRecord)naz_record_v.elementAt(naz_record_v.indexOf((Object)nr0)-j)).getClose();
		    }
		    else {
			mt = 0;
		    }
		}

		ma = mt/j;
		//System.out.println("mt="+mt+"  j="+j);
		break;
	    }
	}

	return ma;
    }

    public int getNazChange(String my_date, Vector naz_record_v) {

	int change = 0;

	Enumeration enum = naz_record_v.elements();
	NasdaqRecord nr0 = new NasdaqRecord ();
	NasdaqRecord nr1 = new NasdaqRecord ();

	while ( enum.hasMoreElements()) { 
	    nr1 = (NasdaqRecord)enum.nextElement();
	    //System.out.println(nr1.getDate());
	    if ((nr1.getDate()).startsWith(my_date)) {
		nr0 = (NasdaqRecord)naz_record_v.elementAt(naz_record_v.indexOf((Object)nr1)-1);
		change = (int)(nr1.getClose()-nr0.getClose());
		System.out.println("nr0.getClose(): "+nr0.getClose());
		System.out.println("nr1.getClose(): "+nr1.getClose());
		//System.out.print("Change for Date:"+my_date+"  is: "+change);
		break;
	    }
	}

	return change;
    }

    public int getNazChange(String my_date) {

	Vector naz_record_v = getNazRecord();
	int change = 0;
	
	Enumeration enum = naz_record_v.elements();
	NasdaqRecord nr0 = new NasdaqRecord ();
	NasdaqRecord nr1 = new NasdaqRecord ();

	while ( enum.hasMoreElements()) { 
	    nr1 = (NasdaqRecord)enum.nextElement();
	    //System.out.println(nr1.getDate());
	    if ((nr1.getDate()).startsWith(my_date)) {
		nr0 = (NasdaqRecord)naz_record_v.elementAt(naz_record_v.indexOf((Object)nr1)-1);
		change = (int)(nr1.getClose()-nr0.getClose());
		//System.out.println("nr0.getClose(): "+nr0.getClose());
		//System.out.println("nr1.getClose(): "+nr1.getClose());
		//System.out.print("Change for Date:"+my_date+"  is: "+change);
		break;
	    }
	}

	return change;
    }
}








